package com.dog.view.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * User: Kuttubek Aidaraliev
 * Date: 08.06.2010
 * Time: 10:46:04
 */
public class BaseDateConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
        throw new ConverterException();
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
        if(o instanceof Date){
            Date date = (Date)o;
            FacesContext context = FacesContext.getCurrentInstance();
            Locale locale = context.getViewRoot().getLocale();
            DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
            return format.format(date);
        }
        throw new IllegalArgumentException("Argument must be Date object!");
    }
}
